package net.mingsoft.cmsMessage.action;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.cmsMessage.biz.ICmsResourcePoolBiz;
import net.mingsoft.cmsMessage.entity.CmsResourcePoolEntity;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;
/**
 * 资源库管理控制层
 * @author ym
 * 创建日期：2022-1-3 22:09:24<br/>
 * 历史修订：<br/>
 */
@Api(value = "资源库接口")
@Controller("cmsmessageCmsResourcePoolAction")
@RequestMapping("/${ms.manager.path}/cmsMessage/cmsResourcePool")
public class CmsResourcePoolAction extends BaseAction{


	/**
	 * 注入资源库业务层
	 */
	@Autowired
	private ICmsResourcePoolBiz cmsResourcePoolBiz;

	/**
	 * 返回主界面index
	 */
	@GetMapping("/index")
	public String index(HttpServletResponse response,HttpServletRequest request) {
		String flag = request.getParameter("flag");
		String name = "";
		if("01".equals(flag)){//图片
			name="/cmsMessage/cms-resource-pool/index_picture";
		}else if("02".equals(flag)){//视频
			name="/cmsMessage/cms-resource-pool/index_video";
		}else{//文件
			name="/cmsMessage/cms-resource-pool/index_file";
		}
		return name;
	}

	/**
	 * 查询资源库列表
	 * @param cmsResourcePool 资源库实体
	 */
	@ApiOperation(value = "查询资源库列表接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "fileName", value = "名称", paramType = "query"),
    })
	@RequestMapping(value ="/list",method = {RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public ResultData list(@ModelAttribute @ApiIgnore CmsResourcePoolEntity cmsResourcePool,HttpServletResponse response, HttpServletRequest request,@ApiIgnore ModelMap model,BindingResult result) {
		BasicUtil.startPage();
		QueryWrapper queryWrapper = new QueryWrapper<CmsResourcePoolEntity>();
		queryWrapper.eq("file_type",cmsResourcePool.getFileType());
		if(StrUtil.isNotEmpty(cmsResourcePool.getFileName())){
			queryWrapper.like("file_name",cmsResourcePool.getFileName().trim());
		}
		queryWrapper.orderByDesc("create_date");
		List cmsResourcePoolList = cmsResourcePoolBiz.list(queryWrapper);
		return ResultData.build().success(new EUListBean(cmsResourcePoolList,(int)BasicUtil.endPage(cmsResourcePoolList).getTotal()));
	}

	/**
	 * 返回编辑界面cmsResourcePool的form
	 */
	@GetMapping("/form")
	public String form(@ModelAttribute CmsResourcePoolEntity cmsResourcePool,HttpServletResponse response,HttpServletRequest request,ModelMap model) {
		return "/cmsMessage/cms-resource-pool/form";
	}


	/**
	 * 获取资源库
	 * @param cmsResourcePool 资源库实体
	 */
	@ApiOperation(value = "获取资源库列表接口")
    @ApiImplicitParam(name = "id", value = "主键ID", required =true,paramType="query")
	@GetMapping("/get")
	@ResponseBody
	public ResultData get(@ModelAttribute @ApiIgnore CmsResourcePoolEntity cmsResourcePool,HttpServletResponse response, HttpServletRequest request,@ApiIgnore ModelMap model) {
		if (cmsResourcePool.getId()==null) {
			return ResultData.build().error();
		}
		CmsResourcePoolEntity _cmsResourcePool = (CmsResourcePoolEntity)cmsResourcePoolBiz.getById(cmsResourcePool.getId());
		return ResultData.build().success(_cmsResourcePool);
	}


	/**
	* 保存资源库
	* @param cmsResourcePool 资源库实体
	*/
	@ApiOperation(value = "保存资源库列表接口")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "fileUrls", value = "文件", required =true, paramType = "query"),
			@ApiImplicitParam(name = "fileName", value = "名称", required =true, paramType = "query"),
	})
	@PostMapping("/save")
	@ResponseBody
	@LogAnn(title = "保存资源库", businessType = BusinessTypeEnum.INSERT)
	@RequiresPermissions("cmsMessage:cmsResourcePool:save")
	public ResultData save(@ModelAttribute @ApiIgnore CmsResourcePoolEntity cmsResourcePool, HttpServletResponse response, HttpServletRequest request) {
		//验证文件的值是否合法
		if (StringUtils.isBlank(cmsResourcePool.getFileUrls())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("file.urls")));
		}
		//验证名称的值是否合法
		if (StringUtils.isBlank(cmsResourcePool.getFileName())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("file.name")));
		}
		if (!StringUtil.checkLength(cmsResourcePool.getFileName()+"", 0, 1024)) {
			return ResultData.build().error(getResString("err.length", this.getResString("file.name"), "0", "1024"));
		}
		cmsResourcePoolBiz.save(cmsResourcePool);
		return ResultData.build().success(cmsResourcePool);
	}

	/**
     *  删除资源库
     *
	 * @param cmsResourcePools 资源库实体
	 */
	@ApiOperation(value = "批量删除资源库列表接口")
	@PostMapping("/delete")
	@ResponseBody
	@LogAnn(title = "删除资源库", businessType = BusinessTypeEnum.DELETE)
	@RequiresPermissions("cmsMessage:cmsResourcePool:del")
	public ResultData delete(@RequestBody List<CmsResourcePoolEntity> cmsResourcePools,HttpServletResponse response, HttpServletRequest request) {
	    List<String> ids = (List)cmsResourcePools.stream().map((p) -> {return p.getId();}).collect(Collectors.toList());
		return this.cmsResourcePoolBiz.removeByIds(ids) ? ResultData.build().success() : ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("id")}));
		}

	/**
	 *	更新资源库列表
	 *
	 * @param cmsResourcePool 资源库实体
	 */
	 @ApiOperation(value = "更新资源库列表接口")
	 @ApiImplicitParams({
    		@ApiImplicitParam(name = "fileUrls", value = "文件", required =true, paramType = "query"),
    		@ApiImplicitParam(name = "fileName", value = "名称", required =true, paramType = "query"),
	})
	@PostMapping("/update")
	@ResponseBody
	@LogAnn(title = "更新资源库", businessType = BusinessTypeEnum.UPDATE)
	@RequiresPermissions("cmsMessage:cmsResourcePool:update")
	public ResultData update(@ModelAttribute @ApiIgnore CmsResourcePoolEntity cmsResourcePool, HttpServletResponse response,
			HttpServletRequest request) {
		//验证文件的值是否合法
		if (StringUtils.isBlank(cmsResourcePool.getFileUrls())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("file.urls")));
		}
		//验证名称的值是否合法
		if (StringUtils.isBlank(cmsResourcePool.getFileName())) {
			return ResultData.build().error(getResString("err.empty", this.getResString("file.name")));
		}
		if (!StringUtil.checkLength(cmsResourcePool.getFileName()+"", 0, 1024)) {
			return ResultData.build().error(getResString("err.length", this.getResString("file.name"), "0", "1024"));
		}
			cmsResourcePoolBiz.updateById(cmsResourcePool);
		return ResultData.build().success(cmsResourcePool);
	}


}
